package com.mall.web.control.app.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.biz.user.record.CollectionBizImpl;
import com.mall.client.user.CollectionService;
import com.mall.pojo.Collection;
import com.mall.pojo.User;
import com.mall.web.annotation.UserSecurity;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/user/conllection/")
public class CollectionController {
	@Autowired
	private CollectionService collectionService;
	@Autowired
	private CollectionBizImpl collectionBiz;
	/**
	 * 用户收藏
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	
	@UserSecurity
	@RequestMapping("collectionAction")
	@ResponseBody
	public ResponseEntity<Collection> collectionaction(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		User user = WebUtil.getCurrentUser(request);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		Integer type = HttpUtil.getIntegerValueFromRequest(request, "type");
		Integer status = HttpUtil.getIntegerValueFromRequest(request, "type");
		Long mallId = HttpUtil.getLongValueFromRequest(request, "status");
		ResponseEntity<Collection> responseEntity = new ResponseEntity<>();
		Collection collection =  this.collectionBiz.collectionMallByMallIdAndOnlyKey(user.getId(),onlyKey,mallId,type,status);
		responseEntity.initSuccResponseEntity(collection, "collection success");
		return responseEntity;
		
	}
	/**
	 * 取消收藏
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	
	@UserSecurity
	@RequestMapping("canleCollectionactionAction")
	@ResponseBody
	public ResponseEntity<Boolean> canleCollectionactionAction(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		Long collectionId = HttpUtil.getLongValueFromRequest(request, "collectionId");
		Collection collection = this.collectionService.getCollectionById(collectionId);
		ResponseEntity<Boolean> responseEntity = new ResponseEntity<>();

		if (collection == null) {
			responseEntity.initErrorResponseEntity(false, "canle error");
		}
		collection.setStatus(4);
		collection.setGmtModify(new Date());
		this.collectionService.modifyCollection(collection);
		responseEntity.initSuccResponseEntity(true, "canle success");
		return responseEntity;

	}
	/**
	 * 收藏列表
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("collectionRecord")
	public String collectionRecord(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		List<Collection> collectionList = this.collectionBiz.collectionRecordMap(request, response, modelMap);
		modelMap.put("collectionList", collectionList);
		return "user/conllection/collectionRecord";
		
	}
	/**
	 * 加载更多
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("collectionRecordList")
	public String collectionRecordList(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		List<Collection> collectionList = this.collectionBiz.collectionRecordMap(request, response, modelMap);
		modelMap.put("collectionList", collectionList);
		return "user/conllection/collectionRecordList";
		
	}
	
	
	

}
